/**
 * Created by WLG on 2018/11/12.
 */
const ServerConfig = require('../config/serverConfig');
let db = {};
let redis = require("async-redis");
let client = redis.createClient(ServerConfig.chatRedisConfig);

client.on("error", function (err) {
    console.log("Error :" , err);
});
client.on("ready", function () {
    commonUtil.consoleSysInfo("Redis清除缓存");
    client.flushdb();
    commonUtil.consoleSysInfo("Redis准备就绪");
});
client.on('connect', function(){
    commonUtil.consoleSysInfo("Redis连接成功");
});

/**
 * 添加string类型的数据
 * @param key 键
 * @params value 值
 * @params expire (过期时间,单位秒;可为空，为空表示不过期)
 */
db.set = async function (key, value, expire) {
    await client.set(key, JSON.stringify(value));
    client.expire(key, parseInt(expire));
};

/**
 * 查询string类型的数据
 * @param key 键
 */
db.get = async function(key){
    let result = await client.get(key);
    if(result){
        result = JSON.parse(result);
    }
    return result;
};
db.del = async function(key){
    return await client.del(key);
};


module.exports = db;